<?php


namespace app\api\controller;

use addons\alisms\Alisms;
use app\admin\model\Admin;
use app\api\model\InfoTypeModel;
use app\api\model\InfoTypeRecord;
use app\api\model\UserInfoModel;
use app\api\model\UserMonitorModel;
use app\api\model\UserTypeBluetooth;
use app\common\controller\Api;
use app\common\library\Sms;
use think\helper\Str;
use think\Model;

class Bluetooth extends api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function toBeSum()
    {
        $sum_str = input('post.sum_str');

        $sum_arr = explode(',', $sum_str);

        $sum = 0;
        foreach ($sum_arr as $val) {
            $dec = base_convert($val, 16, 10);
            $sum += $dec;
        }

        $p1 = base_convert(56, 16, 10);
        $p2 = base_convert('5A', 16, 10);

        $sum = $sum * $p1 + $p2;

        $sum = base_convert($sum, 10, 16);

        $sum = substr($sum, -2);

        $this->success('请求成功', $sum);
    }

    public function sendSumStr()
    {
        $type = input('type');
        $time = input('time');
        //获取温度 2019-12-1 号的历史数据 2C 05 00 01 E3 07 0C 01 XX
        $today = date('Y-m-d', time());
        if ($type == 4) {
            $time = $today;
        }
//        $today = '2021-10-12';
        $today = !empty($time) ? $time : $today;
        $time_arr = explode('-', $today);

        $year_x = base_convert($time_arr[0], 10, 16);
        $month_x = base_convert($time_arr[1], 10, 16);
        $day_x = base_convert($time_arr[2], 10, 16);
        $nowTime = date('H:i:s', time());

        if (strlen($month_x) == 1) {
            $month_x = '0' . $month_x;
        }
        if (strlen($day_x) == 1) {
            $day_x = '0' . $day_x;
        }

        $nowTimeArr = explode(':', $nowTime);
        $h_x = base_convert($nowTimeArr[0], 10, 16);
        $i_x = base_convert($nowTimeArr[1], 10, 16);
        $s_x = base_convert($nowTimeArr[2], 10, 16);

        if (strlen($h_x) == 1) {
            $h_x = '0' . $h_x;
        }
        if (strlen($i_x) == 1) {
            $i_x = '0' . $i_x;
        }
        if (strlen($s_x) == 1) {
            $s_x = '0' . $s_x;
        }

        $i1_arr = str_split($year_x);

        if (count($i1_arr) > 3) {
            $year_x_1 = $i1_arr[2] . $i1_arr[3];
            $year_x_2 = $i1_arr[0] . $i1_arr[1];
        } else {
            $year_x_1 = $i1_arr[1] . $i1_arr[2];
            $year_x_2 = '0' . $i1_arr[0];
        }
        $month_arr = str_split($month_x);
        if (count($month_arr) == 1) {
            $month_x = '0' . $month_x;
        }

        $day_arr = str_split($day_x);
        if (count($day_arr) == 1) {
            $day_x = '0' . $day_x;
        }

        if ($type == 1) {
            //获取温度 2019-12-1 号的历史数据 2C 05 00 01 E3 07 0C 01 XX
            $sum_str = '2C,05,00,01,' . $year_x_1 . ',' . $year_x_2 . ',' . $month_x . ',' . $day_x;
            $sum_str = '2C,01,00,00';
        } elseif ($type == 2) {
            //获取计步 2018-12-1 号的历史数据 20 05 00 01 E2 07 0C 01 XX
            $sum_str = '20,05,00,01,' . $year_x_1 . ',' . $year_x_2 . ',' . $month_x . ',' . $day_x;
        } elseif ($type == 3) {
            //获取心率等 2018-12-1 号的历史数据 21 05 00 01 E2 07 0C 01 XX
            $sum_str = '21,05,00,01,' . $year_x_1 . ',' . $year_x_2 . ',' . $month_x . ',' . $day_x;
        } elseif ($type == 4) {
            //同步当前时间   04 08 00 E2 07 0A 01 0E 00 00 08 XX
            $sum_str = '04,08,00,' . $year_x_1 . ',' . $year_x_2 . ',' . $month_x . ',' . $day_x . ',' . $h_x . ',' . $i_x . ',' . $s_x . ',' . '08';
        }

        $sum_arr = explode(',', $sum_str);

        $sum = 0;
        foreach ($sum_arr as $val) {
            $dec = base_convert($val, 16, 10);
            $sum += $dec;
        }

        $p1 = base_convert(56, 16, 10);
        $p2 = base_convert('5A', 16, 10);

        $sum = $sum * $p1 + $p2;

        $sum = base_convert($sum, 10, 16);

        $sum = substr($sum, -2);

        $sum_str .= ',' . $sum;
        $sum_str = str_replace(',', '', $sum_str);

        $this->success('请求成功', $sum_str);
    }

    /*
     * 获取/操作计步、睡眠记录数据
     */
    public function get_service_info()
    {
        $user = $this->auth->getUser();

        $str = input('post.str');

        $type = input('post.type');

        $time = input('post.time');

        $today = date('Y-m-d', time());

        $time = !empty($time) ? $time : $today;

        $user_id = $user['id'];
        log_print('手环返回数据');
        log_print($time);
        log_print($str);
        log_print($type);

        Db("info_type_timer")->insertGetId([
            'user_id' => $user_id,
            'type' => $type,
            'content' => $str,
            'time' => $time,
            'createtime' => time(),
        ]);

        $this->success('成功');

    }


}